package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.constant.MessageConstant.CATEGORY_BE_RELATED_BY_DISH;
import static com.sky.constant.MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL;
import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询菜品分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {

        //设置页码和页大小
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        //分页查询
        List<Category> categoryList = categoryMapper.pageQuery(categoryPageQueryDTO);
        Page<Category> list = (Page<Category>) categoryList;
        PageResult pageResult = new PageResult(list.getTotal(), list.getResult());

        return Result.success(pageResult);
    }

    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @Override
    public Result save(CategoryDTO categoryDTO) {

        Category category = new Category();

        //属性拷贝
        BeanUtil.copyProperties(categoryDTO,category);

        //设置属性
        category.setStatus(ENABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //添加到数据库
        categoryMapper.save(category);

        return Result.success();
    }

    /**
     * 修改菜品分类
     * @param categoryDTO
     * @return
     */
    @Override
    public Result update(CategoryDTO categoryDTO) {

        Category category = new Category();
        //拷贝属性
        BeanUtil.copyProperties(categoryDTO,category);

        //设置属性
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //修改菜品信息
        categoryMapper.update(category);

        return Result.success();
    }

    /**
     * 启用，禁用分类
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result setStatus(Integer status, Long id) {

        //设置属性
        Category category = Category.builder()
                .status(status)
                .id(id)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                .build();

        //修改状态
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> selectByType(Integer type) {

        //根据类型查询分类
        List<Category> categoryList = categoryMapper.selectByType(type);

        return Result.success(categoryList);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {
        if(dishMapper.dishCount(id)!=0){
            return Result.error(CATEGORY_BE_RELATED_BY_DISH);
        }
        if(setmealMapper.setmealCount(id)!=0){
            return Result.error(CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
        return Result.success();
    }


}
